package com.syezon.wifikey.speedup;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;

import java.util.Random;

public class RainView extends View {

    private static final int RAIN_NUM = 3;//雨滴数量

    private static final int DELAY = 20;//延时
    private Rain[] mRains;//雨滴集合

    private Paint mPaint = new Paint();
    private Paint mBgPaint = new Paint();
    private Path mPath = new Path();
    private int mColor = Color.parseColor("#33FFFFFF");
    private Runnable mRefreshTask = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };

    private Handler mHandler = new Handler(Looper.getMainLooper());

    public RainView(Context context) {
        super(context);
        init();
    }

    public RainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RainView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mColor);

        mBgPaint.setAntiAlias(true);
        mBgPaint.setStyle(Paint.Style.FILL);
//        mBgPaint.setColor(Color.parseColor("#"));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w != oldw || h != oldh) {
            initShow(w, h);
        }
        mPath.addCircle(w / 2, h / 2, w / 2, Path.Direction.CW);
    }

    //视图初始化
    private void initShow(int width, int height) {
        mRains = new Rain[RAIN_NUM];
//        for (int i = 0; i < RAIN_NUM; i++) {
//            mRains[i] = Rain.create(mPaint, width, height);
//        }
        int itemWidth = width / 3;
        Random random = new Random();
        int item1 = random.nextInt(itemWidth);
        int item2 = itemWidth + random.nextInt(itemWidth);
        int item3 = itemWidth * 2 + random.nextInt(itemWidth);
        mRains[0] = Rain.create(mPaint, item1, -10);
        mRains[1] = Rain.create(mPaint, item2, -100);
        mRains[2] = Rain.create(mPaint, item3, -200);

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.clipPath(mPath);
        for (Rain rain : mRains) {
            rain.draw(canvas);
        }
        canvas.drawPath(mPath, mPaint);
        mHandler.postDelayed(mRefreshTask, DELAY);
    }


    public static class Rain {

        private static final float RAIN_SPEED = 25f;
        private static final float RAIN_SIZE = 3;

        private Paint paint;// 画笔
        private float mRainSpeed;// 雨滴下落的增量（速度）
        private float mRainSize;// 雨滴大小
        private int x1, x2, y1, y2;


        public Rain(int x1, int x2, int y1, int y2, Paint mPaint, float mRainSpeed, float mRainSize) {
            super();
            this.x1 = x1;
            this.x2 = x2;
            this.y1 = y1;
            this.y2 = y2;
            this.paint = mPaint;
            this.mRainSpeed = mRainSpeed;
            this.mRainSize = mRainSize;
        }

        /**
         * 创建雨滴类
         *
         * @param paint 画笔
         * @return Rain
         */
        public static Rain create(Paint paint, int startX, int startY) {
            float rainSpeed = RAIN_SPEED;
            float rainSize = RAIN_SIZE;
            return new Rain(startX, startX, startY, startY + 100, paint, rainSpeed, rainSize);
        }

        /**
         * 绘制雨滴类
         *
         * @param canvas
         */
        public void draw(Canvas canvas) {
            int width = canvas.getWidth();
            int height = canvas.getHeight();
            drawLine(canvas, width, height);
        }

        // 画线条
        private void drawLine(Canvas canvas, int width, int height) {
            paint.setStrokeWidth(RAIN_SIZE);
            if (y1 >= height) {
                y1 = -100;
            }
            if (y2 >= (height + 100)) {
                y2 = 0;
            }
            y1 += mRainSpeed;
            y2 += mRainSpeed;
            canvas.drawLine(x1, y1, x2, y2, paint);
        }

    }

    private int dp2px(float dp) {
        return (int) (getResources().getDisplayMetrics().density * dp + 0.5);
    }
}
